class VideosController < ApplicationController
  before_filter :require_admin,           :only => [:new,     :create,  :edit,    :update, :destroy]
  before_filter :find_video,              :only => [:show,    :edit,    :update,  :destroy]
  before_filter :find_list_items,         :only => [:new,     :create,  :show,    :edit]
  before_filter :get_thumb_from_params,   :only => [:create,  :update]
  before_filter :check_video_privacy,     :only => [:show]
  
  def index
    @videos = Video.find(:all, :order => 'created_at DESC').paginate :page => params[:page], :per_page => 8
    respond_to do |format|
      format.html {}
      format.js   { 
        @gallery = Gallery.find(params[:gallery_id]) 
        render :partial => 'galleries/videos', :locals =>{:gallery => @gallery}
      }
    end
  end
  
  def show
    @comment = Comment.new
  end
  
  def new
    @video = Video.new
  end
  
  def create          
    @video = Video.new(params[:video])
    @video.save!
    flash[:update] = 'The video has been uploaded'
    redirect_to video_url(@video)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'  
    render :action => 'new'
  end
  
  def edit
  end
  
  def update
    params[:video][:random] ||= []
    params[:video][:photo_id] ||= []
    params[:video][:is_private] ||= []
    params[:video][:gallery_ids] ||= []
    params[:video][:non_purchasable] ||= []
    @video.update_attributes!(params[:video])
    flash[:update] = 'Information for this video has been updated.'
    redirect_to video_url(@video)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    render :action => 'edit'
  end
  
  def destroy
    @video.destroy
    flash[:update] = 'The video has been deleted.'
    redirect_to videos_url
  end
  
protected
  def check_video_privacy
    if @video.is_private?
      if logged_in?
        unless current_user.authorized_to_view?(@video)
          flash[:error] = "Sorry, but you're not authorized to view that video"
          redirect_back_or_default('/')
        end
      else
        flash[:error] = "Sorry, but you're not authorized to view that video"
        redirect_back_or_default('/')
      end
    end
  end
  def require_admin
    if logged_in?
      unless current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_back_or_default('/')
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_video
    @video = Video.find params[:id]
  end
  def find_list_items
    @thumbnails = Photo.find(:all, :conditions => ["is_thumbnail = ?", true])
    @users = User.find(:all, :conditions => ['is_client = ?', true], :order => 'login desc')
    @galleries = Gallery.find(:all, :order => 'name asc')
  end
  def get_thumb_from_params
    if params[:photo_ids] && params[:photo_ids].length > 1
      flash[:error] = 'You can only check one thumbnail image'
      redirect_back_or_default('/')
    elsif params[:photo_ids]
      params[:video][:photo_id] = params[:photo_ids].first
    else
      params[:video][:photo_id] ||= []
    end
  end
end
